package a_;

import org.junit.Test;

import java.io.*;

public class BufferedDemo1 {

    @Test//文件的加密和解密
    public void test() throws IOException {
        FileInputStream fis = new FileInputStream("a_bak.txt");
        FileOutputStream fos = new FileOutputStream("a_bak_a.txt");

        int len;
        while ((len = fis.read()) != -1) {
            fos.write(len ^ 1);
        }
    }

    @Test//异常处理
    public void test2() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("a.txt"));
            bos = new BufferedOutputStream(new FileOutputStream("a_bak.txt"));
            int len;
            while ((len = bis.read()) != -1) {
                bos.write(len ^ 1);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void test3(){
        try(BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream("a.txt"));
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream("a_bak.txt"))){

            int len;
            while((len = bis.read()) != -1){
                //写入内容
                bos.write(len ^ 1);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}